package com.huyu.substat.modules.substat.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.easy.excel.vo.FieldValue;
import org.springframework.web.context.WebApplicationContext;

import com.thinkgem.jeesite.common.utils.Util;

public class BaseSubjectService {
	private static final Logger logger = Logger.getLogger(BaseSubjectService.class);
	protected WebApplicationContext appContext;

	protected List<FieldValue> getFieldValues(String className, Map<String, String> table) {
		List<FieldValue> rl = new ArrayList<FieldValue>();
		List<Map<String, String>> tableCols = this.getTableCols(Util.o2s(table.get("id")));
		for (Map<String, String> col : tableCols) {
			FieldValue fv = new FieldValue();
			fv.setName(col.get("javaField"));
			fv.setTitle(col.get("comments"));
			rl.add(fv);
		}

		return rl;
	}

	protected List<Map<String, String>> getTableCols(String genTableId) {
		String sql = "select * from gen_table_column where gen_table_id=? and is_list='1' and del_flag='0' order by sort";
		PreparedStatement pstmt = null;
		Connection conn = null;
		ResultSet rs = null;
		List<Map<String, String>> rl = new ArrayList<Map<String, String>>();
		try {
			conn = getConn();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, genTableId);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Map<String, String> rm = new HashMap<String, String>();
				rm.put("comments", rs.getString("comments"));
				rm.put("id", rs.getString("id"));
				rm.put("name", rs.getString("name"));
				rm.put("javaField", rs.getString("java_field"));
				rm.put("comments", rs.getString("comments"));
				rl.add(rm);
			}
		} catch (SQLException e) {
			logger.error("getTableMap error", e);
		} finally {
			Util.closePreparedStatement(pstmt);
			Util.closeConnection(conn);
		}
		return rl;
	}

	protected Map<String, String> getTableMap(String className) {
		String sql = "select * from gen_table where class_name=? and del_flag='0' order by create_date desc";
		PreparedStatement pstmt = null;
		Connection conn = null;
		ResultSet rs = null;
		Map<String, String> rm = new HashMap<String, String>();
		try {
			conn = getConn();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, className);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				rm.put("comments", rs.getString("comments"));
				rm.put("id", rs.getString("id"));
				rm.put("name", rs.getString("comments"));
				rm.put("className", rs.getString("class_name"));
				rm.put("comments", rs.getString("comments"));
			}
		} catch (SQLException e) {
			logger.error("getTableMap error", e);
		} finally {
			Util.closePreparedStatement(pstmt);
			Util.closeConnection(conn);
		}
		return rm;
	}

	protected Connection getConn() throws SQLException {
		DataSource ds = (DataSource) appContext.getBean("dataSource");
		return ds.getConnection();
	}
}
